#include <iostream>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include "person.pb.h"
using namespace std;

string pro_str;
void set_person()
{
    Person obj;
    obj.set_name("mike");
    obj.set_id(1);
    //obj.set_email("mike@qq.com")

    *obj.mutable_email()="mike@qq.com";


    int fd = open("pro_fd",O_RDWR | O_CREAT,0644);
    if (fd < 0)
    {
        perror("open");
        exit(0);
    }
    obj.SerializeToFileDescriptor(fd);
    close(fd);
}

void get_person()
{
    int fd = open("pro_fd",O_RDONLY);
    if(fd <0 )
    {
        perror("open err when get");
        exit(0);
    }
    Person p;
    p.ParseFromFileDescriptor(fd);

    cout << "name:"<<p.name()<<endl;
    cout << "id:"<<p.id()<<endl;
    cout << "email:"<<p.email()<<endl;


}

int main()
{
    GOOGLE_PROTOBUF_VERIFY_VERSION;
    set_person();
    get_person();
    google::protobuf::ShutdownProtobufLibrary();
    return 0;
}
